<?php

namespace App\Imports;

use App\Model\CcmallOrderLogistic;
use App\Model\OldOrder;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class OrderImport implements ToCollection
{
    public function collection(Collection $rows)
    {
        foreach ($rows as $row) {
            $byCode = mb_substr($row[0], 0, 2);
            if ($byCode == 'O1') {
                $model = CcmallOrderLogistic::query()->where('order_code', $row[0])->first();
                if ($model === null) {
                    $model = new CcmallOrderLogistic();
                    $model->order_code  = $row[0];
                    $model->logistics_platform = $this->conversion($row[1]);
                    $model->logistics_code = $row[2];
                    $model->operator_code = 'tzl';
                    $model->operator_type = 2;
                    $model->save();
                } else {
                    $model->logistics_platform = $this->conversion($row[1]);
                    $model->logistics_code = $row[2];
                    $model->operator_code = 'tzl';
                    $model->operator_type = 2;
                    $model->updated_at = date('Y-m-d H:i:s');
                    $model->save();
                }
            } else {
                OldOrder::query()->where('code', $row[0])->update([
                    'logistics_provider' => $this->conversion($row[1]),
                    'logistics_code' => $row[2],
                ]);
            }
        }
    }

    public function conversion($str)
    {
        switch ($str) {
            case '韵达';
                return 'yundaex.com';
            case '邦宝韵达';
                return 'yundaex.com';
            case 'JD韵达快递';
                return 'yundaex.com';
            case '圆通';
                return 'yto.net.cn';
            case '申通';
                return 'sto.cn';
            case '申通快递';
                return 'sto.cn';
            case '邮政小包-邮政快递包裹';
                return 'kuaidi100.com';
            case '邮政';
                return 'kuaidi100.com';
            case '百世';
                return '800bestex.com';
            case '百世快递';
                return '800bestex.com';
            case '顺丰';
                return 'sf-express.com';
            case '顺丰快递';
                return 'sf-express.com';
            case '天天':
                return 'ttkdex.com';
            case '天天快递':
                return 'ttkdex.com';
            case 'EMS':
                return 'ems.com.cn';
            default:
                return $str;
        }
    }
}
